package cn.com.twoke.application.game.mario.core.ecs.component.render.text;

import cn.com.twoke.application.game.mario.core.ecs.component.PositionComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.SizeComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.render.RenderComponentHandler;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;
import cn.com.twoke.application.game.mario.game.config.Tiles;

import java.awt.*;
import java.awt.image.BufferedImage;

public class TextRenderComponentHandler implements RenderComponentHandler<TextRenderComponent> {
    @Override
    public void handle(Entity entity, BufferedImage container, Graphics2D g, TextRenderComponent component) {
        if (entity.hasComponent(PositionComponent.class) && entity.hasComponent(SizeComponent.class)) {
            PositionComponent position = entity.getComponent(PositionComponent.class);
            SizeComponent size = entity.getComponent(SizeComponent.class);
            FontMetrics fm = g.getFontMetrics();
            g.setColor(component.getColor());
            g.drawString(component.getText(), (int) position.x + size.width / 2 - fm.stringWidth(component.getText()) / 2  + Tiles.scaleOf(component.getOffsetX()), (int) position.y + Tiles.scaleOf(component.getOffsetY()));
        }
    }
}
